package com.yonyou.brigade.util.oss;

import com.yonyou.iuap.file.FileManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * 工具类
 *
 * @author kangfw5
 * @since 2019-07-06
 */
@Slf4j
@Component
public class AliyunOssUtil {

    @Value("${storeBucket}")
    private String bucketName;

    /**
     * 上传文件
     *
     * @param key         文件key
     * @param inputStream
     * @return
     */
    public String upload(String key, InputStream inputStream) {
        String filePath = "";
        try {
            FileManager.uploadFileWithoutUUID(bucketName, key, inputStream);
            filePath = getUrl(key);
        } catch (Exception ex) {
            log.error("file upload failed:", ex);
        }
        return filePath;
    }

    /**
     * 生成URL
     *
     * @param key 文件key
     * @return
     */
    public String getUrl(String key) {
        try {
            String accessAddress = FileManager.getUrlWithHttps(bucketName, key, 10);
            return accessAddress;
        } catch (Exception ex) {
            log.error("get file url failed");
        }
        return null;
    }
}
